package vshevchuk;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.Graphics2D; 
import java.awt.image.BufferedImage; 
import java.io.File; 
import java.io.IOException; 
import javax.imageio.ImageIO; 
import javax.swing.JColorChooser;
import javax.swing.JFrame; 
import javax.swing.JPanel; 

public class PaintGui implements MouseListener, MouseMotionListener
{
	private JFrame Form = new JFrame("Paint");
	
	private JPanel panel = new JPanel();
	private BufferedImage image;
	private Graphics2D graph;
	
	private Color ColorGraund = Color.white;
	private Point line = new Point(0, 0);
	private Color ColorPen = Color.black;
	private int Thickness=1;
	
	private class ActionChangeColor implements ActionListener 
	{
		public void actionPerformed(ActionEvent e) 
		{
			ColorPen=JColorChooser.showDialog(null, null, ColorPen);
		}
	}
	
	private class ActionChangeThickness implements ActionListener 
	{
		public void actionPerformed(ActionEvent e) 
		{
			JMenuItem Clist = (JMenuItem)e.getSource();
			Thickness=Integer.parseInt(Clist.getText());
		}
	}
	
	private class ActionSave implements ActionListener 
	{
		public void actionPerformed(ActionEvent e) 
		{
			try 
			{
		    	ImageIO.write(image, "png", new File("Paint.png")); 
		    } 
		    catch (IOException io) 
		    { 
		    	io.printStackTrace(); 
		    } 
		}
	}
	
	private class ActionLoad implements ActionListener 
	{
		public void actionPerformed(ActionEvent e) 
		{
		    try 
		    {    
		    	image = ImageIO.read( new File("Paint.png")); 
		    } 
		    catch (IOException io) 
		    { 
		    	io.printStackTrace(); 
		    }
		    RepaintPanel();
		}
	}
	
	private void CreatMenu () 
	{	
		int i=0;
		
		JMenuBar menuBar = new JMenuBar();
		Font font = new Font("Verdana", Font.PLAIN, 11);
		
		JMenu[] Menu = {new JMenu("File"), new JMenu("Color"), new JMenu("Thickness line")};
	//	JMenuItem[][] Item = {{new JMenuItem("Load"),new JMenuItem("Save")},{new JMenuItem("Change color")}};
		
		for (JMenu menu:Menu)
		{
			menuBar.add(menu);
		}
		
		
		JMenuItem LoadItem = new JMenuItem("Load");
		Menu[0].add(LoadItem);
		LoadItem.setFont(font);
		LoadItem.addActionListener(new ActionLoad());
		
		JMenuItem SaveItem = new JMenuItem("Save");
		Menu[0].add(SaveItem);
		SaveItem.setFont(font);
		SaveItem.addActionListener(new ActionSave ());
		
		JMenuItem ColorItem = new JMenuItem("Change color");
		Menu[1].add(ColorItem);
		ColorItem.setFont(font);
		ColorItem.addActionListener(new ActionChangeColor());
				
		for (i=1; i<20; i+=2)
		{
			JMenuItem ThicknessItem = new JMenuItem(String.valueOf(i));
			Menu[2].add(ThicknessItem);
			ThicknessItem.setFont(font);
			ThicknessItem.addActionListener(new ActionChangeThickness());
		}
		
		Form.add("North",menuBar);
		Form.add(panel);
	}
			
	public PaintGui() 
	{
		CreatMenu();
			
		Form.setSize(500, 500);
		Form.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Form.setVisible(true); 
		
		panel.setBackground(ColorGraund);
		image = new  BufferedImage(panel.getWidth(),  panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
		panel.addMouseListener(this);
		panel.addMouseMotionListener(this);	
	}
		
	public void mouseClicked(MouseEvent e) 
	{
	}
	public void mouseEntered(MouseEvent e) 
	{
	}
	public void mouseExited(MouseEvent e) 
	{
	}
	public void mouseReleased(MouseEvent e) 
	{
	}
	public void mouseMoved(MouseEvent e) 
	{
	}
	
	private void RepaintPanel ()
	{
		graph = (Graphics2D)panel.getGraphics();
		graph.drawImage(image, 0, 0, panel);
	}
		
	public void mousePressed (MouseEvent e) 
	{
		line.move(e.getX(), e.getY());
	}
		
		
	public void mouseDragged (MouseEvent e) 
	{
		graph = (Graphics2D) image.getGraphics();
		
		graph.setStroke(new BasicStroke(Thickness));
		graph.setColor(ColorPen);
		graph.drawLine(line.x, line.y, e.getX(), e.getY());
		line.move(e.getX(), e.getY());
		
		RepaintPanel();		
	}
	
	
	public static void main (String[] args) 
	{
		new PaintGui();
	}
}